function formatTime2(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()


	return [year, month, day].map(formatNumber).join('-')
}

function dateAddDays(date) {
	date = date.replace(/\-/g, '\/'); //日期参数 格式new Date("2017/09/12")所有浏览器都兼容，new Date("2017-09-12") 部分IE不兼容
	var dd = new Date(date);
	dd.setDate(dd.getDate() + 1);
	var year = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期 
	var d = dd.getDate();
	var mon = (m < 10) ? ('0' + m) : ('' + m);
	var day = (d < 10) ? ('0' + d) : ('' + d);
	return year + "-" + mon + "-" + day;
}

function dateAddDayz(date) {
	date = date.replace(/\-/g, '\/'); //日期参数 格式new Date("2017/09/12")所有浏览器都兼容，new Date("2017-09-12") 部分IE不兼容
	var dd = new Date(date);
	dd.setDate(dd.getDate() - 1);
	var year = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期 
	var d = dd.getDate();
	var mon = (m < 10) ? ('0' + m) : ('' + m);
	var day = (d < 10) ? ('0' + d) : ('' + d);
	return year + "-" + mon + "-" + day;

	return pdate;
}

function dateDiffIncludeToday(startDateString, endDateString) {
	var oDate1 = new Date(startDateString);
	var oDate2 = new Date(endDateString);
	if (oDate1.getTime() > oDate2.getTime()) {
		return 1;
	} else {
		return 2;
	}
}

function jianDate(date, days) {
	var d = new Date(date);
	d.setDate(d.getDate() - days);
	var m = d.getMonth() + 1;
	return d.getFullYear() + '-' + m + '-' + d.getDate();
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}

function formatDate(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	// if (date.getHours() >= 24) {
	//   day = date.getDate()+24
	// }

	return [year, month, day].map(formatNumber).join('-')
}

function adddate(date, num) {
	var dd = new Date(date);
	dd.setDate(dd.getDate() + num); //获取AddDayCount天后的日期
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期
	var d = dd.getDate();
	return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d)
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}
/**
 * 天气编码与天气图片对应关系
 */
var codeAndPicWeather = [{
		code: "00",
		name: "晴",
		dayPic: "晴.png",
		nightPic: "晴-夜晚.png",
		bgDayPic: "白天-多云.png",
		bgNightPic: "夜晚-多云.png"
	},
	{
		code: "01",
		name: "多云",
		dayPic: "多云.png",
		nightPic: "多云-夜晚.png",
		bgDayPic: "白天-多云.png",
		bgNightPic: "夜晚-多云.png"
	},
	{
		code: "02",
		name: "阴",
		dayPic: "阴.png",
		nightPic: "阴.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "03",
		name: "阵雨",
		dayPic: "小雨.png",
		nightPic: "小雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "04",
		name: "雷阵雨",
		dayPic: "雷阵雨.png",
		nightPic: "雷阵雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "05",
		name: "冰雹",
		dayPic: "雷阵雨伴冰雹.png",
		nightPic: "雷阵雨伴冰雹.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "06",
		name: "雨夹雪",
		dayPic: "雨夹雪.png",
		nightPic: "雨夹雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "07",
		name: "小雨",
		dayPic: "小雨.png",
		nightPic: "小雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "08",
		name: "中雨",
		dayPic: "中雨.png",
		nightPic: "中雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "09",
		name: "大雨",
		dayPic: "大雨.png",
		nightPic: "大雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "10",
		name: "暴雨",
		dayPic: "暴雨.png",
		nightPic: "暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "11",
		name: "大暴雨",
		dayPic: "大暴雨.png",
		nightPic: "大暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "12",
		name: "特大暴雨",
		dayPic: "特大暴雨.png",
		nightPic: "特大暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "13",
		name: "阵雪",
		dayPic: "阵雪.png",
		nightPic: "阵雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "14",
		name: "小雪",
		dayPic: "小雪.png",
		nightPic: "小雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "15",
		name: "中雪",
		dayPic: "中雪.png",
		nightPic: "中雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "16",
		name: "大雪",
		dayPic: "大雪.png",
		nightPic: "大雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "17",
		name: "暴雪",
		dayPic: "大暴雪.png",
		nightPic: "大暴雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "18",
		name: "雾",
		dayPic: "雾.png",
		nightPic: "雾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "19",
		name: "冻雨",
		dayPic: "冻雨.png",
		nightPic: "冻雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "20",
		name: "沙尘暴",
		dayPic: "浮尘.png",
		nightPic: "浮尘.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "21",
		name: "小到中雨",
		dayPic: "中雨.png",
		nightPic: "中雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "22",
		name: "中到大雨",
		dayPic: "大雨.png",
		nightPic: "大雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "23",
		name: "大到暴雨",
		dayPic: "暴雨.png",
		nightPic: "暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "24",
		name: "暴雨到大暴雨",
		dayPic: "大暴雨.png",
		nightPic: "大暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "25",
		name: "大暴雨到特大暴雨",
		dayPic: "特大暴雨.png",
		nightPic: "特大暴雨.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "26",
		name: "小到中雪",
		dayPic: "中雪.png",
		nightPic: "中雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "27",
		name: "中到大雪",
		dayPic: "大雪.png",
		nightPic: "大雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "28",
		name: "大到暴雪",
		dayPic: "大暴雪.png",
		nightPic: "大暴雪.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "29",
		name: "浮尘",
		dayPic: "浮尘.png",
		nightPic: "浮尘.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "30",
		name: "扬沙",
		dayPic: "浮尘.png",
		nightPic: "浮尘.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "31",
		name: "强沙尘暴",
		dayPic: "浮尘.png",
		nightPic: "浮尘.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "53",
		name: "霾",
		dayPic: "霾.png",
		nightPic: "霾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "54",
		name: "中度霾",
		dayPic: "霾.png",
		nightPic: "霾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "55",
		name: "重度霾",
		dayPic: "霾.png",
		nightPic: "霾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "56",
		name: "严重霾",
		dayPic: "霾.png",
		nightPic: "霾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "57",
		name: "大雾",
		dayPic: "雾.png",
		nightPic: "雾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	},
	{
		code: "58",
		name: "特强浓雾",
		dayPic: "雾.png",
		nightPic: "雾.png",
		bgDayPic: "白天-阴.png",
		bgNightPic: "夜晚-阴.png"
	}
];

function findPicWeatherByCode(code) {
	for (var i = 0; i < codeAndPicWeather.length; i++) {
		if (codeAndPicWeather[i].code == code) {
			return codeAndPicWeather[i];
		}
	}
	return null;
}

//判断两个日期间隔
function comparedate(sDate, eDate) {
	var sdate = new Date(sDate);
	var edate = new Date(eDate);
	var comp = Math.abs(sdate.getTime() - edate.getTime());
	return Math.floor(comp / 86400000);
}
// 农历日期
var lunar = {
	tg: '甲乙丙丁戊己庚辛壬癸',
	dz: '子丑寅卯辰巳午未申酉戌亥',
	number: '一二三四五六七八九十',
	year: '鼠牛虎兔龙蛇马羊猴鸡狗猪',
	month: '正二三四五六七八九十冬腊',
	monthadd: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
	calendar: [
		0xa4b,
		0x5164b,
		0x6a5,
		0x6d4,
		0x415b5,
		0x2b6,
		0x957,
		0x2092f,
		0x497,
		0x60c96,
		0xd4a,
		0xea5,
		0x50da9,
		0x5ad,
		0x2b6,
		0x3126e,
		0x92e,
		0x7192d,
		0xc95,
		0xd4a,
		0x61b4a,
		0xb55,
		0x56a,
		0x4155b,
		0x25d,
		0x92d,
		0x2192b,
		0xa95,
		0x71695,
		0x6ca,
		0xb55,
		0x50ab5,
		0x4da,
		0xa5b,
		0x30a57,
		0x52b,
		0x8152a,
		0xe95,
		0x6aa,
		0x615aa,
		0xab5,
		0x4b6,
		0x414ae,
		0xa57,
		0x526,
		0x31d26,
		0xd95,
		0x70b55,
		0x56a,
		0x96d,
		0x5095d,
		0x4ad,
		0xa4d,
		0x41a4d,
		0xd25,
		0x81aa5,
		0xb54,
		0xb6a,
		0x612da,
		0x95b,
		0x49b,
		0x41497,
		0xa4b,
		0xa164b,
		0x6a5,
		0x6d4,
		0x615b4,
		0xab6,
		0x957,
		0x5092f,
		0x497,
		0x64b,
		0x30d4a,
		0xea5,
		0x80d65,
		0x5ac,
		0xab6,
		0x5126d,
		0x92e,
		0xc96,
		0x41a95,
		0xd4a,
		0xda5,
		0x20b55,
		0x56a,
		0x7155b,
		0x25d,
		0x92d,
		0x5192b,
		0xa95,
		0xb4a,
		0x416aa,
		0xad5,
		0x90ab5,
		0x4ba,
		0xa5b,
		0x60a57,
		0x52b,
		0xa93,
		0x40e95
	]
};

// 农历日期
function getLunarDate(date) {
	var year, month, day;
	if (!date) {
		(date = new Date()), (year = date.getFullYear()), (month = date.getMonth()), (day = date.getDate());
	} else {
		(date = date.split('-')), (year = parseInt(date[0])), (month = date[1] - 1), (day = parseInt(date[2]));
	}
	if (year < 1921 || year > 2021) {
		return {};
	}
	var total, m, n, k, bit, lunarYear, lunarMonth, lunarDay;
	var isEnd = false;
	var tmp = year;
	if (tmp < 1900) {
		tmp += 1900;
	}
	total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + lunar.monthadd[month] + day - 38;
	if (year % 4 == 0 && month > 1) {
		total++;
	}
	for (m = 0;; m++) {
		k = lunar.calendar[m] < 0xfff ? 11 : 12;
		for (n = k; n >= 0; n--) {
			bit = (lunar.calendar[m] >> n) & 1;
			if (total <= 29 + bit) {
				isEnd = true;
				break;
			}
			total = total - 29 - bit;
		}
		if (isEnd) break;
	}
	lunarYear = 1921 + m;
	lunarMonth = k - n + 1;
	lunarDay = total;
	if (k == 12) {
		if (lunarMonth == Math.floor(lunar.calendar[m] / 0x10000) + 1) {
			lunarMonth = 1 - lunarMonth;
		}
		if (lunarMonth > Math.floor(lunar.calendar[m] / 0x10000) + 1) {
			lunarMonth--;
		}
	}
	return {
		lunarYear: lunarYear,
		lunarMonth: lunarMonth,
		lunarDay: lunarDay
	};
}
// 农历日期
function getLunarDateString(lunarDate) {
	if (!lunarDate.lunarDay) return;
	var data = {},
		lunarYear = lunarDate.lunarYear,
		lunarMonth = lunarDate.lunarMonth,
		lunarDay = lunarDate.lunarDay;
	data.tg = lunar.tg.charAt((lunarYear - 4) % 10);
	data.dz = lunar.dz.charAt((lunarYear - 4) % 12);
	data.year = lunar.year.charAt((lunarYear - 4) % 12);
	data.month = lunarMonth < 1 ? '(闰)' + lunar.month.charAt(-lunarMonth - 1) : lunar.month.charAt(lunarMonth - 1);

	data.day = lunarDay < 11 ? '初' : lunarDay < 20 ? '十' : lunarDay < 30 ? '廿' : '三十';
	if (lunarDay % 10 != 0 || lunarDay == 10) {
		data.day += lunar.number.charAt((lunarDay - 1) % 10);
	}
	return data;
}
module.exports = {
	comparedate: comparedate,
	formatTime: formatTime,
	formatDate: formatDate,
	formatTime2: formatTime2,
	dateAddDays: dateAddDays,
	dateAddDayz: dateAddDayz,
	dateDiffIncludeToday: dateDiffIncludeToday,
	findPicWeatherByCode: findPicWeatherByCode,
	adddate: adddate,
	getLunarDate: getLunarDate,
	getLunarDateString: getLunarDateString
}
